<?php
// +----------------------------------------------------------------------
// | XiaMiCMF - 代码如诗
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://shujuwajue.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小虾米 <509129@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

use Common\Controller\AdminBaseController;

class AdController extends AdminBaseController
{
    const CNAME = '广告';

    protected $ad_model;

    public function _initialize() {
        parent::_initialize();
        $this->ad_model = D("Common/Ad");
        $this->assign('TITLE', self::CNAME);
    }

    /**
     * 网站广告列表
     */
    public function index() {
        $list = $this->ad_model->fetchAll();
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 添加网站广告
     */
    public function add() {
        if (IS_POST) {
            $data = $this->ad_model->create();
            if ($data) {
                $id = $this->ad_model->add($data);
                if ($id) {
                    $this->success('添加成功', U('index'));
                } else {
                    $this->error('添加失败');
                }
            } else {
                $this->error($this->ad_model->getError());
            }
        } else {
            $this->display();
        }
    }

    /**
     * 编辑网站广告
     */
    public function edit() {
        if (IS_POST) {
            $data = $this->ad_model->create();
            if ($data) {
                if ($this->ad_model->save($data) !== false) {
                    $this->success('编辑成功', U('index'));
                } else {
                    $this->error('编辑失败');
                }
            } else {
                $this->error($this->ad_model->getError());
            }
        } else {
            $ad_id  = I('get.ad_id', 0, 'intval');
            $detail = $this->ad_model->find($ad_id);
            $this->assign('detail', $detail);
            $this->display();
        }
    }

    /**
     * 删除友情链接
     */
    public function delete() {
        $ad_id = I('post.ad_id',0,'intval');

        if (is_numeric($ad_id)) {
            $this->ad_model->delete($ad_id);
            $this->success('删除成功', U('index'));
        } elseif (is_array($ad_id)) {
            $this->ad_model->delete(implode(',', $ad_id));
            $this->success('删除成功', U('index'));
        } else {
            $this->error('请选择要删除的' . self::CNAME);
        }
    }

    /**
     * 显示或不显示广告
     */
    public function changeStatus() {

        $ad_id = I('post.ad_id', 0, 'intval');

        if (!empty($ad_id)) {

            if (!$detail = $this->ad_model->find($ad_id)) {
                $this->error('请选择要改变状态的' . self::CNAME);
            }
            $data['ad_id']  = $ad_id;
            $data['status'] = $detail['status'] ? 0 : 1 ;
            if ($this->ad_model->save($data) !== false) {
                $this->success(self::CNAME . '状态改变成功', U('index'));
            } else {
                $this->error(self::CNAME . '状态改变失败');
            }

        } else {
            $this->error('请选择要改变状态的' . self::CNAME);
        }
    }
}